package com.sskj.one;

import com.sskj.lib.BaseHttpConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.one.WebPresenter;

import android.content.Context;
import android.content.Intent;
import android.widget.TextView;

import com.sskj.one.R;
import com.sskj.one.bean.AgreeBean;
import com.zzhoujay.richtext.RichText;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.concurrent.CancellationException;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/05/24
 */
public class WebActivity extends BaseActivity<WebPresenter> {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.web_content)
    TextView webContent;

    private String type;

    @Override
    public int getLayoutId() {
        return R.layout.app_activity_web;
    }

    @Override
    public WebPresenter getPresenter() {
        return new WebPresenter();
    }

    @Override
    public void initView() {
        type = getIntent().getStringExtra("type");
        switch (type) {
            case "reg_agree":
                tvTitle.setText("购汇须知");
                break;
            case "trans_agree":
                tvTitle.setText("结汇须知");
                break;
            case "trade_rule":
                tvTitle.setText("新手须知");
                break;
            case "about_aml":
                tvTitle.setText("关于我们");
                break;
            case "law_agree":
                tvTitle.setText("股池规则");
                break;
            case "register_agree":
                tvTitle.setText("注册协议");
                break;
            default:
                break;
        }
    }

    @Override
    public void initData() {
        mPresenter.getAgree(type);

    }

    public static void start(Context context, String type) {
        Intent intent = new Intent(context, WebActivity.class);
        intent.putExtra("type", type);
        context.startActivity(intent);
    }

    public void getAgreeOK(AgreeBean data) {
        if (data == null) {
            return;
        }
        RichText.fromHtml(formatImg(data.getInc_type())).into(webContent);
    }

    private String formatImg(String law_agree) {
        Document document = Jsoup.parse(law_agree);
        Elements imgs = document.getElementsByTag("img");
        for (Element img : imgs) {
            img.attr("width", "100%").attr("height", "auto");
            String url = img.attr("src");
            if (!url.contains("http")) {
                url = BaseHttpConfig.BASE_URL + url;
            }
            img.attr("src", url);
        }
        return document.toString();
    }
}
